/* * * * M A P V I S I O N   L I C E N S E  *****************************************************************
 *
 * This MapVision software is © Filip Zavadil 2013. It is licensed under the LGPL license.
 * This MapVision software dynamically links to unmodified Qt5 Library.
 *
 * The Qt5 Library is © 2013 Qt® and/or its subsidiary(-ies), and is licensed under the
 * GNU Lesser General Public License version 2.1 with Qt® LGPL exception version 1.1.
 * Qt5 library is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
 * Qt5 library is provided “AS IS”, without WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
 * As an additional permission to the GNU Lesser General Public License version 2.1, the object
 * code form of a “work that uses the Library” may incorporate material from a header file that is
 * part of the Library. You may distribute such object code under terms of your choice,
 * provided that: (i) the header files of the Library have not been modified; and (ii)
 * the incorporated material is limited to numerical parameters, data structure layouts, accessors,
 * macros, inline functions and templates; and (iii) you comply with the terms of Section 6 of the
 * GNU Lesser General Public License version 2.1.
 *
 * Moreover, you may apply this exception to a modified version of the Library, provided that such
 * modification does not involve copying material from the Library into the modified Library’s header
 * files unless such material is limited to (i) numerical parameters; (ii) data structure layouts;
 * (iii) accessors; and (iv) small macros, templates and inline functions of five lines or less in length.
 *
 * Furthermore, you are not required to apply this additional permission to a modified version of the Library.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this package;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

 * The source code for Qt 5.X SDK is available from Qt Project here:
 * http://download.qt-project.org/official_releases/qtcreator/2.7/2.7.0/qt-creator-2.7.0-src.zip.
 *
 * ****************************** M A P V I S I O N    S O F T W A R E  ************************************
 *
 * Author:       Filip Zavadil
 * Description:  Software for displaying and exporting map tiles.
 * The sources and executables of MapVision software are available:
 *    - from project hosting on CodePlex.com :   http://mapvision.codeplex.com
 *    - from GIT repositories on Gitorious.org:  https://gitorious.org/mapvision
 * Visit MapVision fanpage on Facebook.com:      https://www.facebook.com/pages/Mapvision/631827753513935
 *
 * *********************************************************************************************************
*/

#include "mapvision.h"
#include "ui_mapvision.h"
#include "dialogpreferences.h"
#include "layertileslocal.h"
#include "layertilesonline.h"
#include "layerimg.h"



MapVision::MapVision(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MapVision)
{
    ui->setupUi(this);

    exporting = new Export(this,&layers);
    document = new MapDocumet(this,&layers);

    ui->listWidgetLayers->setLayers(&layers);

    connect(ui->map,SIGNAL(zoomChanged(int)),this,SLOT(zoomHasChanged(int)));
    connect(ui->map,SIGNAL(viewScaleChanged(int)),this,SLOT(viewScaleHasChanged(int)));
    connect(ui->map,SIGNAL(mouseOnLonlat(QString)),this,SLOT(mouseOn(QString)));
    connect(ui->map,SIGNAL(message(QString)),this,SLOT(printMessage(QString)));
    connect(ui->map,SIGNAL(mapUpdated(QPointF,int,int)),this,SLOT(mapUpdated(QPointF,int,int)));
    connect(ui->map,SIGNAL(panEnabledChanged(bool)),this,SLOT(panEnabledHasChanged(bool)));


    ui->map->setCenterLonLat(QPointF(0,0));

    movie = new QMovie("images/planetLoading.gif");
    ui->labelIsLoading->setMovie(movie);



    QActionGroup* group = new QActionGroup( this );
    group->setObjectName("actionsGroup");
    ui->actionMake_Selection->setActionGroup(group);
    ui->actionHandPan->setActionGroup(group);
    ui->actionMousePointer->setActionGroup(group);
    ui->actionPointer->setActionGroup(group);


    ui->dockWidget->hide();

    DockWidgetCoorsInfo * dockCoorsInfo = new DockWidgetCoorsInfo(this, ui->actionPointer);
    this->addDockWidget(Qt::RightDockWidgetArea,dockCoorsInfo,Qt::Vertical);

    DockWidgetEffects * dockEffects = new DockWidgetEffects(this);
    this->addDockWidget(Qt::RightDockWidgetArea,dockEffects,Qt::Vertical);

    DockWidgetZoomTo * dockZoomTo = new DockWidgetZoomTo(this);
    this->addDockWidget(Qt::RightDockWidgetArea,dockZoomTo,Qt::Vertical);

    DockWidgetSelectionInfo * dockSelectionInfo = new DockWidgetSelectionInfo(this);
    this->addDockWidget(Qt::RightDockWidgetArea,dockSelectionInfo,Qt::Vertical);

    DockWidgetAddMapFeatures * dockMapFeatures = new DockWidgetAddMapFeatures(this);
    this->addDockWidget(Qt::RightDockWidgetArea,dockMapFeatures,Qt::Vertical);

    DockWidgetText * dockText = new DockWidgetText(this);
    this->addDockWidget(Qt::RightDockWidgetArea,dockText,Qt::Vertical);

    this->on_actionMousePointer_toggled(true);


}

MapVision::~MapVision()
{
    delete ui;
}

void MapVision::keyPressEvent(QKeyEvent *event)
{

    switch (event->key()) {
    case Qt::Key_S:
        break;
    case  Qt::Key_Control:
        ;
        break;
    default:
        break;
    }

    QMainWindow::keyPressEvent(event);
}

void MapVision::mouseReleaseEvent(QMouseEvent *)
{

}

void MapVision::zoomHasChanged(int value)
{
    ui->spinBoxZoom->setValue(value);
}

void MapVision::viewScaleHasChanged(int value)
{
    ui->spinBoxView->setValue(value);
}

void MapVision::mouseOn(QString dms)
{
    ui->lineEditCoors->setText(dms);
}

void MapVision::printMessage(QString message)
{
    ui->textBrowserLogConsole->setHtml(ui->textBrowserLogConsole->toHtml()+message+"<br/>\n");
    ui->textBrowserLogConsole->verticalScrollBar()->setValue(ui->textBrowserLogConsole->verticalScrollBar()->maximum());
}


void MapVision::consoleSave()
{
    QFileDialog fileDialog;
    fileDialog.setDefaultSuffix(".txt");
    QString fileName = fileDialog.getSaveFileName(this, tr("Save image map"), settings.value("save/mapImage").toString(),
                                                  tr("HTML page (*.html);;PlainText (*.txt);;RichText (*.rtf);;All Files (*.*)"));

    if (fileName == ""){
        QMessageBox msgBox;
        msgBox.setText(tr("No filename specified!"));
        msgBox.exec();
        return;
    } else {
        QFile *textak =  new QFile(fileName);
        if (textak->open(QIODevice::ReadWrite)){
            QTextStream out(textak);
            if (fileName.contains(".txt")){
                out << ui->textBrowserLogConsole->toPlainText();
            } else if (fileName.contains(".html")||fileName.contains(".rtf")||fileName.contains(".pdf")){
                out << ui->textBrowserLogConsole->toHtml();
            } else {
                out << ui->textBrowserLogConsole->toPlainText();
            }


        }

        textak->close();
    }
}
void MapVision::consoleClear()
{
    ui->textBrowserLogConsole->clear();
}


void MapVision::mapUpdated(QPointF, int, int)
{
    ui->labelScale->setText(ui->map->getMapScale());
}

void MapVision::panEnabledHasChanged(bool toogle_)
{
    ui->actionHandPan->setChecked(toogle_);
}

void MapVision::on_spinBoxView_editingFinished()
{
    ui->map->setViewScale(ui->spinBoxView->value());
}

void MapVision::on_actionPreferences_triggered()
{
    DialogPreferences dPreferences(this);
    dPreferences.setModal(false);
    dPreferences.exec();
}

void MapVision::on_checkBoxRender_toggled(bool checked)
{
    Q_UNUSED(checked);
    ui->map->update();
}

void MapVision::on_action100_triggered()
{
    ui->map->setViewScale(100);
}

void MapVision::on_action150_triggered()
{
    ui->map->setViewScale(150);
}

void MapVision::on_action200_triggered()
{
    ui->map->setViewScale(200);
}

void MapVision::on_action10_triggered()
{
    ui->map->setViewScale(10);
}

void MapVision::on_action25_triggered()
{
    ui->map->setViewScale(25);
}

void MapVision::on_action75_triggered()
{
    ui->map->setViewScale(75);
}

void MapVision::on_action50_triggered()
{
    ui->map->setViewScale(50);
}

void MapVision::on_spinBoxZoom_editingFinished()
{
    ui->map->setZoom(ui->spinBoxZoom->value());
}

void MapVision::on_actionAbout_triggered()
{
    About * aboutScreen = new About();
    aboutScreen->show();

}

void MapVision::on_actionTEST_triggered()
{
    QList<int> zooms;
    zooms << 1;
    exporting->exportTiles(zooms);
}


void MapVision::on_actionAs_image_triggered()
{
    exporting->makePicture();
}

void MapVision::on_actionSelect_by_coordinates_triggered()
{
    ui->map->selection->showDialog();
    ui->actionMake_Selection->toggle();
}

void MapVision::on_actionInfo_triggered()
{
    ui->map->selection->showInfo();
}

void MapVision::on_actionLocal_Tiles_triggered()
{
    layers.push_back(new LayerTILESlocal(this));
}

void MapVision::on_actionOnline_Tiles_triggered()
{
    layers.push_back(new LayerTILESonline(this));
}

void MapVision::on_actionSQLite_databes_triggered()
{
    layers.push_back(new LayerSQLite(this));
}

void MapVision::on_actionIMG_triggered()
{
    layers.push_back(new LayerIMG(this));
}

void MapVision::on_action_Open_triggered()
{
    if (document->open2()) ui->listWidgetLayers->updateOrder();
}

void MapVision::on_actionSave_triggered()
{
    document->save(false);
}

void MapVision::on_actionSave_As_triggered()
{
    document->save(true);
}

void MapVision::on_actionHandPan_toggled(bool arg1)
{
    ui->map->setPanEnabled(arg1);
    if (arg1){
        // ui->actionHandPan->setIcon(QIcon("images/buttons/handClosed.svg"));
        ui->map->viewport()->setCursor(Qt::OpenHandCursor);
        ui->map->lastCursor = Qt::OpenHandCursor;
        ui->map->mapViewMode = MapView::PAN;
    } else {
        ui->actionHandPan->setIcon(QIcon("images/buttons/handOpen.svg"));
    }
}

void MapVision::on_actionMake_Selection_toggled(bool arg1)
{
    ui->map->selection->isActive(arg1);

    if (arg1){
        //   ui->actionMake_Selection->setIcon(QIcon("images/buttons/selectionChecked.svg"));
        ui->map->viewport()->setCursor(Qt::CrossCursor);
        ui->map->lastCursor = Qt::CrossCursor;
        ui->map->mapViewMode = MapView::SELECT_AREA;
    } else {
        ui->actionMake_Selection->setIcon(QIcon("images/buttons/selection.svg"));
    }
}

void MapVision::on_actionMousePointer_toggled(bool arg1)
{
    if (arg1){
        // ui->actionMousePointer->setIcon(QIcon("images/buttons/mousePointerActive.svg"));
        ui->map->viewport()->setCursor(Qt::ArrowCursor);
        ui->map->lastCursor = Qt::ArrowCursor;
        ui->map->mapViewMode = MapView::SELECT_ITEMS;
    } else {
        ui->actionMousePointer->setIcon(QIcon("images/buttons/mousePointer.svg"));
    }
}


void MapVision::on_actionZoomPlus_triggered()
{
    ui->map->setZoom(ui->map->getZoom()+1);
}

void MapVision::on_actionZoomMinus_triggered()
{
    ui->map->setZoom(ui->map->getZoom()-1);
}

void MapVision::on_actionPointer_toggled(bool arg1)
{
    if (arg1){
        //  ui->actionPointer->setIcon(QIcon("images/buttons/pointerSelected.svg"));
        ui->map->viewport()->setCursor(QPixmap("images/buttons/mousePointerCursor.png"));
        ui->map->lastCursor = QPixmap("images/buttons/mousePointerCursor.png");
        ui->map->mapViewMode = MapView::POINTER;
    } else {
        ui->actionPointer->setIcon(QIcon("images/buttons/pointer.svg"));
    }
}

void MapVision::on_actionViewMinus_triggered()
{
    ui->map->setViewScale(ui->spinBoxView->value()-10);
}

void MapVision::on_actionViewPlus_triggered()
{
    ui->map->setViewScale(ui->spinBoxView->value()+10);
}

void MapVision::on_actionRefresh_triggered()
{
    ui->map->refresh();
}


void MapVision::on_actionTEST_2_triggered()
{
    exporting->showDialog();
}

void MapVision::on_actionWelcome_screen_triggered()
{
    this->ui->map->getWelcome()->addWelcome();
}



void MapVision::on_actionQuit_triggered()
{
    this->close();
}
